package com.jframe.sys.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author l2iu11
 * <p>json-lib-2.4.jdk15.jar</p>
 * <p>commons-logging-1.1.1.jar</p>
 * <p>commons-collections-3.2.1.jar</p>
 * <p>ezmorph-1.0.6.jar</p>
 * <p>commons-beanutils-1.8.3.jar</p>
 */
public class JsonUtil {
	
	
	 /**
     * 从一个JSON 对象字符格式中得到一个java对象
     * @param jsonString
     * @param pojoCalss
     * @return
     */
    public static Object json2Obj(String jsonStr,Class clazz){
    	
        Object pojo;
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
        pojo = JSONObject.toBean(jsonObject,clazz);
        return pojo;
    }   
    
     
    /**
     * 从json HASH表达式中获取�?��map，改map支持嵌套功能
     * @param jsonString
     * @return
     */
    public static Map json2Map(String jsonStr){
    	
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
        Iterator  keyIter = jsonObject.keys();
        String key;
        Object value;
        Map map = new HashMap();
        while( keyIter.hasNext())
        {
            key = (String)keyIter.next();
            value = jsonObject.get(key);
            map.put(key, value);
        }
        return map;
    }   
    
    /**
     * 将缓存中的json转换为List《map》
     * @param jsonStr
     * @return
     */
    public static List<Map> json2ListMap(String jsonStr){
    	List<Map> list = new ArrayList<Map>();
    	JSONArray jsonArray = JSONArray.fromObject(jsonStr);
    	for(int i = 0 ; i<jsonArray.size(); i++){   
    		JSONObject jsonObject = jsonArray.getJSONObject(i);
    		Iterator  keyIter = jsonObject.keys();
    		 Map map = new HashMap();
    		 String key;
    		 Object value;
	        while( keyIter.hasNext())
	        {
	            key = (String)keyIter.next();
	            value = jsonObject.get(key);
	            map.put(key, value);
	        }
	        list.add(map);
    	}
    	return list;
    }
    
   
    
    
    /** 
     * 从json对象集合表达式中得到�?��java对象列表
     * @param jsonString
     * @param pojoClass
     * @return
     */
    public static List json2List(String jsonStr, Class clazz){
        if(jsonStr==null){
        	return new ArrayList();
        }else{
	        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
	        JSONObject jsonObject;
	        Object obj;
	        List list = new ArrayList();
	        for ( int i = 0 ; i<jsonArray.size(); i++){           
	            jsonObject = jsonArray.getJSONObject(i);
	            obj = JSONObject.toBean(jsonObject,clazz);
	            list.add(obj);
	        }
	        return list;
        }
    }
    
    /**
     * 获取json中关键字为key的所有值。
     * @param json
     * @param key
     * @return
     */
  //  public static List json2KeyList(String json,String key){
    	
   // }
    
    
    /**
     * 从json数组中得到相应java数组
     * @param jsonString
     * @return
     */
    public static Object[] json2ObjArray(String jsonStr){
    	
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        return jsonArray.toArray();
    }
    
    
    /**
     * 从json数组中解析出java字符串数�?
     * @param jsonStr
     * @return
     */
    public static String[] json2StringArray(String jsonStr){
        
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        String[] stringArray = new String[jsonArray.size()];
        for( int i = 0 ; i<jsonArray.size() ; i++ ){
            stringArray[i] = jsonArray.getString(i);           
        }
        return stringArray;
    }
    
    
    /**
     * 从json数组中解析出javaLong型对象数�?
     * @param jsonStr
     * @return
     */
    public static Long[] json2LongArray(String jsonStr){
        
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        Long[] longArray = new Long[jsonArray.size()];
        for( int i = 0 ; i<jsonArray.size() ; i++ ){
            longArray[i] = jsonArray.getLong(i);            
        }
        return longArray;
    }
    
    
    /**
     * 从json数组中解析出java Integer型对象数�?
     * @param jsonStr
     * @return
     */
    public static Integer[] json2IntArray(String jsonStr){
        
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        Integer[] integerArray = new Integer[jsonArray.size()];
        for( int i = 0 ; i<jsonArray.size() ; i++ ){
            integerArray[i] = jsonArray.getInt(i);           
        }
        return integerArray;
    }
    
       
    /**
     * 将java对象转换成json字符�?支持List、Object、Map�?
     * @param obj
     * @return
     */
    public static String obj2Json(Object obj){     
    	
        JSONObject json;
        json = JSONObject.fromObject(obj);
        return json.toString();       
    }
    
    
    /**
     * 将List转换成json字符�?
     * @param list
     * @return
     */
    public static String list2Json(List list){    	
    	return JSONArray.fromObject(list).toString();
    }
    
    /**
     * 将Map转换成json字符�?
     * @param map
     * @return
     */
    public static String map2Json(Map map){
    	return obj2Json(map); 
    }
    
    /**
     * 获取json串中关键字段key的�?
     * @param json
     * @param key（关键字�?
     * @return
     */
    public static String getJsonValue(String json ,String key)
	{
		String value = "";
		String[] jsonArray = json2StringArray(json);		
		for(int i =0 ;i < jsonArray.length ;i++)
		{
			String s = jsonArray[i];
			int idx = s.indexOf(":");
			if(idx==-1) continue;
			String k = s.substring(0,idx);
			String v=s.substring(idx+1 );  
			if(StringUtil.null2Space(k).equalsIgnoreCase(key)){
				value =v;
				break;
			}
		}
		return value;
	}
    
    public static String array2json(Object object){
    	return JSONArray.fromObject(object).toString();
    }


	public static Set<String> getJsonKeySet(String json) {
		// TODO Auto-generated method stub
		return null;
	}
 
    
    /**
     * @param args
     */
   /* public static void main(String[] args) {
        // TODO 自动生成方法存根
    	JsonUtil jsonUtil = new JsonUtil();
    	   	User user = new User();
    	   	user.setUserId(1);
    	   	user.setUserName("刘");
    	   //	String json = "{userNo:1,userName:'刘伟扬'}";
    	   //	User u = (User) jsonUtil.json2Obj(json, User.class);
    	   	List<User> ls = new ArrayList<User>();
    	   	ls.add(user);
    	//Map map = new TreeMap();
    	//map.put("attributes", "{url=menu/toMaintain}");
    	//map.put("children", "[{id=102, text=路灯管理-根据�?��ID查看配电�? attributes={url=adminManage/findBoxByBureauId}, checked=true}, {id=103, text=路灯管理-根据�?��ID查看道路, attributes={url=adminManage/findWayByBureauId}, checked=true}, {id=104, text=路灯管理-�?��是否有双灯头, attributes={url=adminManage/findLampBydisboxId}, checked=true}, {id=101, text=�?��管理-异步查询上级�?��, attributes={url=bureau/getAllBurears}, checked=true}, {id=1, text=维护人员管理主页, attributes={url=man/tomaintainmanindex}, checked=true}, {id=2, text=维护人员-添加, attributes={url=man/addMan}, checked=true}, {id=3, text=维护人员-删除, attributes={url=man/deleteMan}, checked=true}, {id=4, text=维护人员-修改, attributes={url=man/modifyMan}, checked=true}, {id=5, text=维护人员-查询�?��, attributes={url=man/getCurrentPageOfMan}, checked=true}, {id=6, text=维护人员与配电箱关联管理主页, attributes={url=manbox/tomaintainboxindex}, checked=true}, {id=7, text=维护人员与配电箱关联管理-添加, attributes={url=manbox/addMainBox}, checked=true}, {id=8, text=维护人员与配电箱关联管理-删除, attributes={url=manbox/deleteMainBox}, checked=true}, {id=9, text=维护人员与配电箱关联管理-修改, attributes={url=manbox/modifyMainBox}, checked=true}, {id=10, text=维护人员与配电箱关联管理-查询�?��, attributes={url=manbox/getCurrentPageOfMainbox}, checked=true}, {id=11, text=配电箱管�?查询�?��, attributes={url=box/getBoxes}, checked=true}, {id=22, text=奖励管理主页, attributes={url=encouragement/toencouragement}, checked=true}, {id=23, text=奖励管理-添加, attributes={url=encouragement/addEncouragement}, checked=true}, {id=24, text=奖励管理-删除, attributes={url=encouragement/deleteEncouragement}, checked=true}, {id=25, text=奖励管理-修改, attributes={url=encouragement/modifyEncouragement}, checked=true}, {id=26, text=奖励管理-查询�?��, attributes={url=encouragement/getCurrentPageOfEncouragement}, checked=true}, {id=27, text=维护工单管理主页, attributes={url=dispatch/todispatch}, checked=true}, {id=28, text=维护工单管理-添加, attributes={url=dispatch/addDispatch}, checked=true}, {id=29, text=维护工单管理-删除, attributes={url=dispatch/deleteDispatch}, checked=true}, {id=30, text=维护工单管理-修改, attributes={url=dispatch/modifyDispatch}, checked=true}, {id=31, text=维护工单管理-按id修改, attributes={url=dispatch/modifyByIdDispatch}, checked=true}, {id=32, text=维护工单管理-查询�?��, attributes={url=dispatch/getCurrentPageOfDispatch}, checked=true}, {id=33, text=维护工单管理-按id查询, attributes={url=dispatch/getByIdDispatch}, checked=true}, {id=34, text=工程验收管理主页, attributes={url=projectm/toprojectm}, checked=true}, {id=35, text=工程验收管理-添加, attributes={url=projectm/addProjectm}, checked=true}, {id=36, text=工程验收管理-删除, attributes={url=projectm/deleteProjectm}, checked=true}, {id=37, text=工程验收管理-修改, attributes={url=projectm/modifyPMAction}, checked=true}, {id=38, text=工程验收管理-查询�?��, attributes={url=projectm/getCurrentPageOfProjectm}, checked=true}, {id=58, text=�?��管理主页, attributes={url=bureau/tobureauindex}, checked=true}, {id=59, text=�?��管理-添加, attributes={url=bureau/saveBureau}, checked=true}, {id=60, text=�?��管理-删除, attributes={url=bureau/deleteBureau}, checked=true}, {id=61, text=�?��管理-修改, attributes={url=bureau/updateBureau}, checked=true}, {id=62, text=�?��管理-查询�?��, attributes={url=bureau/getAllBur}, checked=true}, {id=63, text=�?��管理-查询id、nane, attributes={url=bureau/getBureauIdAndName}, checked=true}, {id=64, text=�?��管理-查询上级�?��, attributes={url=bureau/getUpperBureaus}, checked=true}, {id=65, text=配电箱管理主�? attributes={url=disbox/todisboxindex}, checked=true}, {id=66, text=配电箱管�?添加, attributes={url=disbox/saveDistributionBox}, checked=true}, {id=67, text=配电箱管�?删除, attributes={url=disbox/deleteDistributionBox}, checked=true}, {id=68, text=配电箱管�?修改, attributes={url=disbox/updateDistributionBox}, checked=true}, {id=69, text=配电箱管�?查询�?��, attributes={url=disbox/getAllDisbox}, checked=true}, {id=70, text=路灯管理主页, attributes={url=lamp/tolampindex}, checked=true}, {id=71, text=路灯管理-添加, attributes={url=lamp/saveLamp}, checked=true}, {id=72, text=路灯管理-删除, attributes={url=lamp/deleteLamp}, checked=true}, {id=73, text=路灯管理-修改, attributes={url=lamp/updateLamp}, checked=true}, {id=74, text=路灯管理-查询�?��, attributes={url=lamp/getAllLamps}, checked=true}]");		
		System.out.println(JsonUtil.list2Json(ls));
    }*/
  
}
